﻿using System;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQWEB.huongnghiep.BL;

namespace VQWEB.huongnghiep.handlers
{
    /// <summary>
    /// Summary description for NewsHandler
    /// </summary>
    public class NewsHandler : IHttpHandler, IRouteHandler
    {
        ProfessionData ProfessionData = new ProfessionData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            string url = context.Request.UrlReferrer.ToString();
            Int32 newsId = Ulties.GetRequestId(url, "i", ".html");
            
            //1. View & related news
            if (newsId > 0)
            {
                var objNews = ProfessionData.GetNewsById(Convert.ToInt32(newsId));
                int categoryId = Ulties.GetRequestId(url,"c","i");
                var objCategory = ProfessionData.GetCategoryById(categoryId);

                var output = new
                                 {
                                     objNews.news_id,
                                     objNews.news_title,
                                     news_image = string.IsNullOrEmpty(string.IsNullOrEmpty(objNews.news_image)
                                                                  ? "/img/no.jpg": objNews.news_image),
                                     objNews.news_description,
                                     objNews.news_content,
                                     objNews.news_hit,
                                     objNews.news_date,
                                     objNews.news_active,
                                     category_title = objCategory == null ? " -- " : objCategory.category_title,
                                     category_url = objCategory == null ? "#":Ulties.FormatNewsCategoryUrl(objCategory.category_id,objCategory.category_title),
                                     related = ProfessionData.GetRelatedNews(categoryId, newsId, 8)
                                 };

                var json = serializer.Serialize(output);
                context.Request.ContentType = "application/json";
                context.Response.Write(json);


            }
            //2. List
            else
            {
                int categoryId = Ulties.GetRequestId(url, "-", ".html");
                if(Ulties.IsNumeric(context.Request["curPage"]))
                {
                    Int32 curPage = Convert.ToInt32(context.Request["curPage"]);
                    Int32 pageSize = Convert.ToInt32(context.Request["pageSize"]);
                    string json = serializer.Serialize(ProfessionData.GetNewsByCategoryId(categoryId, curPage, pageSize));
                    context.Request.ContentType = "application/json";
                    context.Response.Write(json);
                }
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext RequestContext)
        {
            return this;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}